package francis.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import francis.model.VideoInfo;
import francis.utils.VideoUtil;
import francis.utils.WebTools;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;

public class HomeActivity extends Activity {
	List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();;
	Button wholeSiteBtn;
	Button newSetBtn;
	int showNo = Home_Show.HOME_SHOW_SITE;
	String URL = "";
	Bitmap bitmapReview;
	Bitmap bitmapVideoReview;
 	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		bitmapReview = BitmapFactory.decodeResource(this.getResources(), R.drawable.home_review_img);
		bitmapVideoReview = BitmapFactory.decodeResource(this.getResources(), R.drawable.home_video_review_img);
	 
		checkShow();
		resetListView();
		BtnOnClickListener l = new BtnOnClickListener();
		wholeSiteBtn = (Button)this.findViewById(R.id.wholeSite);
		wholeSiteBtn.setOnClickListener(l);
		newSetBtn = (Button)this.findViewById(R.id.newSet);
		newSetBtn.setOnClickListener(l);
	}
 	
 	public class Home_Show
 	{
 		static final int HOME_SHOW_SITE = 0;
 		static final int HOME_SHOW_SET  = 1;
 	}
 	
 	public class BtnOnClickListener implements OnClickListener
 	{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Button btnButton = (Button)v;
			if (btnButton == wholeSiteBtn) 
			{
				showNo = Home_Show.HOME_SHOW_SITE;
				wholeSiteBtn.setBackgroundResource(R.drawable.home_btn_bg);
				newSetBtn.setBackgroundResource(R.drawable.home_btn_bg_null);
			}
			else if(btnButton == newSetBtn)
			{
				showNo = Home_Show.HOME_SHOW_SET;
				newSetBtn.setBackgroundResource(R.drawable.home_btn_bg);
				wholeSiteBtn.setBackgroundResource(R.drawable.home_btn_bg_null);
			}
			checkShow();
			resetListView();
		}
 		
 	}
 	
 	private void checkShow()
 	{
 		if (showNo == Home_Show.HOME_SHOW_SITE) 
 		{
			URL = "http://api.bilibili.tv/list?type=xml&tid=1";

		}
 		else 
 		{
 			URL = "http://api.bilibili.tv/list?type=xml&tid=13";

		}
 	}

 	private void resetListView()
 	{
 		List<VideoInfo> vInfos = VideoUtil.getVideoInfos(URL);
 		data.clear();
		for(int i = 0; i< vInfos.size(); i++) 
		{	
			VideoInfo info = vInfos.get(i);
			Bitmap bitmap = WebTools.returnBitMap(info.getPic());
			
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("url", bitmap);
			map.put("title",info.getTitle());
			map.put("review",bitmapReview);
			map.put("review_num", "    " + info.getReview());
			map.put("video_review", bitmapVideoReview);
			map.put("video_review_num", "    " + info.getVideo_review());
			map.put("list_no", i + 1 + ".");
			data.add(map);
		}
		
		SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.home_item,
				new String[]{"url","title","review","review_num",
				             "video_review","video_review_num","list_no"},
				new int[]{R.id.hotVedio_img, R.id.hotVedio_title,
							  R.id.share_img,      R.id.shar_num,
							  R.id.comment_img,  R.id.comment_num,
							  R.id.list_no});
		
		adapter.setViewBinder(new ViewBinder() {
			@Override
			public boolean setViewValue(View view, Object data,
					String textRepresentation) {
				
				if(view instanceof ImageView && data instanceof Bitmap) {
					ImageView iv = (ImageView)view;
					iv.setImageBitmap((Bitmap)data);
					return true;
				}
				return false;
			}
		});
		ListView contentView = (ListView)this.findViewById(R.id.hot_list);
		
		contentView.setAdapter(adapter);
 	}
}
